package top.hmtools.jsManager;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 缺省的javascript文件管理者
 * @author HyboJ
 * 创建日期：2017-9-26下午9:42:56
 */
public class JavascriptManagerDefault implements IJavascriptManager{
	
	protected final Logger logger = LoggerFactory.getLogger(JavascriptManagerDefault.class);
	
	/**
	 * javascript文件缓存库
	 */
	private static Map<String, String> JS_REPERTORY = new HashMap<String, String>();
	
	private List<File> filePaths ;
	
	private String encoding;

	@Override
	public void init(List<File> filePaths,String encoding) {
		//初始化文件路径集合与文件编码格式，用于刷新
		if(filePaths != null && filePaths.size()>0){
			this.filePaths = filePaths;
		}
		if(encoding != null && !"".equals(encoding)){
			this.encoding = encoding;
		}
		
		//将指定文件夹中的所有js文件内容读取到缓存
		String[] extensions = {"js"}; 
		for(File pathTmp:filePaths){
			try {
				if(!pathTmp.isDirectory()||!pathTmp.exists()){
					continue;
				}
				Collection<File> jsFilesTmp = FileUtils.listFiles(pathTmp, extensions, true);
				for(File fileTmp:jsFilesTmp){
					String fileName = fileTmp.getName();
					String fileContent = FileUtils.readFileToString(fileTmp, encoding);
					JS_REPERTORY.put(fileName, fileContent);
				}
			} catch (Exception e) {
				e.printStackTrace();
				logger.error(e.getMessage());
				logger.error("获取"+pathTmp.getAbsolutePath()+"目录下的js文件失败");
			}
		}
	}

	@Override
	public void destory() {
		if(JS_REPERTORY != null){
			JS_REPERTORY.clear();
		}
	}

	@Override
	public String getJs(String jsNames) {
		if(jsNames != null && !"".equals(jsNames)){
			String[] jsNamesArr = jsNames.split(",");
			return this.getJs(jsNamesArr);
		}else{
			return ";";
		}
	}

	@Override
	public String getJs(List<String> jsNames) {
		if(jsNames != null && jsNames.size()>0){
			String[] jsNamesArr = jsNames.toArray(new String[0]);
			return this.getJs(jsNamesArr);
		}else{
			return ";";
		}
	}

	@Override
	public String getJs(String[] jsNames) {
		StringBuffer sb_result = new StringBuffer(";");
		if(jsNames != null && jsNames.length>0){
			for(String fileName:jsNames){
				String js_content_tmp = JS_REPERTORY.get(fileName.trim().toLowerCase());
				if(js_content_tmp == null || "".equals(js_content_tmp)){
					continue;
				}
				sb_result.append(js_content_tmp+";");
			}
			return sb_result.toString();
		}else{
			return ";";
		}
	}

	@Override
	public boolean refresh() {
		boolean result = false;
		try {
			this.destory();
			this.init(filePaths, encoding);
			result=true;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return result;
	}

	@Override
	public List<String> listJsFilenames() {
		List<String> result = new ArrayList<String>();
		if(JS_REPERTORY != null){
			Set<String> keySet = JS_REPERTORY.keySet();
			result.addAll(keySet);
			Collections.sort(result);
		}
		return result;
	}

}
